'use strict'
require('console-color-mr')
const express = require('express')
const lessMiddleware = require('less-middleware')
const routes = require('./routes')
const apiRouter = require('./routes/apiRouter.js')
const path = require('path')
const favicon = require('express-favicon')
const app = express()

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

app.use(express.json())
app.use(express.urlencoded({ extended: false }))

app.use(lessMiddleware(path.join(__dirname, 'public')))
app.use(express.static(path.join(__dirname, 'public')))
app.use(favicon(path.join(__dirname, './public/image//fav.ico')))

app.use('/', routes)
// app.use('/api', apiRouter)

app.use((req, res, next) => {
  next(createError(404))
})

app.use((err, req, res, next) => {
  res.locals.message = err.message
  res.locals.error = req.app.get('env') === 'development' ? err : {}

  res.status(err.status || 500)
  res.render('error')
})

// app.all('*', function(req, res, next) {
//   res.header('Access-Control-Allow-Origin', '*')
//   //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行
//   res.header('Access-Control-Allow-Headers', 'Content-Type')
//   res.header('Access-Control-Allow-Methods', '*')
//   res.header('Content-Type', 'application/json;charset=utf-8')
//   next()
// })

app.listen(5501, () =>
  console.log(
    '\n\nApp running at: \n- Local: ',
    ' http://127.0.0.1:5500'.cyan.underline,
    ' \n- Network:',
    'http://192.168.50.31:5500'.underline.cyan,
    '\n'
  )
)
